﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
using Microsoft.Wireless;
using System.Text.RegularExpressions;

namespace GoogleVoice
{
    public partial class Settings : Form
    {
        /// <summary>
        /// Setup the panel, get the phone number from the sim card.
        /// </summary>
        public Settings()
        {
            InitializeComponent();
            this.LoadSettings(); // Load the previous settings.
            try
            {
                if(this.gvMyNumber.Text.Length != 10)
                    this.gvMyNumber.Text = Sim.GetPhoneNumber().Address;
                //this.defaultNumber.Enabled = false; // If it's a phone, the number is set...
                // but what if they want to connect to a different phone, you say?
            }
            catch (Exception ex)
            {
                // I couldn't get the number, leave it alone.
            }
        }
        /// <summary>
        /// Load settings from the registry
        /// </summary>
        private void LoadSettings()
        {
            this.gvUsername.Text = (String)Registry.GetValue(Constants.REG_PATH, "gvUsername", "");
            this.gvPassword.Text = (String)Registry.GetValue(Constants.REG_PATH, "gvPassword", "");
            this.gvMyNumber.Text = (String)Registry.GetValue(Constants.REG_PATH, "gvMyNumber", "");
        }
        /// <summary>
        /// Save the settings to the registry.  This happens on closing the form.
        /// </summary>
        /// <param name="sender">The Setting form closing.</param>
        /// <param name="e">Cancel the close.</param>
        private void SaveSettings(object sender, CancelEventArgs e)
        {
            Registry.SetValue(Constants.REG_PATH, "gvUsername", this.gvUsername.Text);
            Registry.SetValue(Constants.REG_PATH, "gvPassword", this.gvPassword.Text);
            String gvMyNumber = PhoneUtils.CleanPhoneText(this.gvMyNumber.Text);
            if (gvMyNumber.Length == 10)
            {
                Registry.SetValue(Constants.REG_PATH, "gvMyNumber", this.gvMyNumber.Text);
            }
            else
            {
                MessageBox.Show("Your phone number must be 10 digits long");
                e.Cancel = true;
            }
        }
    }
}